package com.abigdreamer.im.client.module.main.weather;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.Map;

import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.abigdreamer.swing.EmptyComponent;
import com.abigdreamer.swing.app.SwingUtil;
import com.abigdreamer.swing.component.JCLabel;
import com.abigdreamer.util.MyCalendar;

import craky.layout.LineLayout;

public class WeatherPanel extends JDialog {

	private static final long serialVersionUID = 6753542278975429187L;

	// 实现背景图片，放在LayeredPane上
	private void createBackGround() {
		Icon background = SwingUtil.getIcon("weather/weatherPanelBg.png");// 背景图片
		JLabel label = new JLabel(background);// 把背景图片显示在一个标签里面
		// 把标签的大小位置设置为图片刚好填充整个面板
		label.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
		// 把内容窗格转化为JPanel，否则不能用方法setOpaque()来使内容窗格透明
		JPanel imagePanel = (JPanel) getContentPane();
		imagePanel.setOpaque(false);
		// 内容窗格默认的布局管理器为BorderLayout
		//imagePanel.setLayout(new FlowLayout());
		//imagePanel.add(new JButton("测试按钮"));
		getLayeredPane().setLayout(null);
		// 把背景图片添加到分层窗格的最底层作为背景
		getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
		
		setSize(background.getIconWidth(), background.getIconHeight());
	}

	public WeatherPanel(WeatherLabel weatherLabel) {

		setSize(new Dimension(240, 210));
		//
		setUndecorated(true);

		createBackGround();

		EmptyComponent ecHeadLabel = new EmptyComponent();
		ecHeadLabel.setLayout(new LineLayout(10, 8, 0, 5, 0, LineLayout.CENTER, LineLayout.LEADING, LineLayout.VERTICAL));
		ecHeadLabel.setPreferredSize(new Dimension(-1, 25));
		ecHeadLabel.add(new JCLabel("南京"), LineLayout.START);

		JCLabel lblWeather = new JCLabel();
		lblWeather.setIcon(SwingUtil.getIcon("weather/" + weatherLabel.weatherImg + ".png", 60, 60));
		ecHeadLabel.add(lblWeather, LineLayout.START);

		Map<String, Map<String, String>> weatherReport = weatherLabel.weatherReport;

		if (weatherReport != null) {
			Box emptyComponent = Box.createVerticalBox();
			emptyComponent.setAlignmentX(Box.LEFT_ALIGNMENT);
			// EmptyComponent emptyComponent = new EmptyComponent();
			// emptyComponent.setLayout(new LineLayout(0, 0, 0, 0, 0,
			// LineLayout.CENTER, LineLayout.LEADING, LineLayout.VERTICAL));
			// emptyComponent.setPreferredSize(new Dimension(-1, 25));
			emptyComponent.add(new JCLabel(weatherReport.get("current_conditions").get("temp_c") + "℃"));// ,
																											// LineLayout.START);
			emptyComponent.add(new JCLabel(weatherReport.get("current_conditions").get("condition")));// ,
																										// LineLayout.START);

			String[] coditions = weatherReport.get("current_conditions").get("wind_condition").split("、");
			for (int i = 0; i < coditions.length; i++) {
				emptyComponent.add(new JCLabel(coditions[i]));// ,
																// LineLayout.START);
			}

			EmptyComponent ecCurrentWeather = new EmptyComponent();
			emptyComponent.setPreferredSize(new Dimension(-1, 76));
			ecCurrentWeather.setLayout(new GridLayout(1, 3));

			ecCurrentWeather.add(ecHeadLabel);
			ecCurrentWeather.add(emptyComponent);
			ecCurrentWeather.add(new JCLabel("(实时天气)"));

			EmptyComponent ecForecastWeather = new EmptyComponent();
			ecForecastWeather.setLayout(new GridLayout(1, 3));
			ecForecastWeather.setPreferredSize(new Dimension(-1, 64));

			ecForecastWeather.add(getForecastWeatherComponent(weatherReport.get("forecast_conditions1"), "今天"), LineLayout.START);
			ecForecastWeather.add(getForecastWeatherComponent(weatherReport.get("forecast_conditions2"), "明天"), LineLayout.START);
			ecForecastWeather.add(getForecastWeatherComponent(weatherReport.get("forecast_conditions3"), "后天"), LineLayout.START);

			EmptyComponent ecWeather = new EmptyComponent();
			ecWeather.setLayout(new LineLayout(5, 0, 0, 0, 0, LineLayout.CENTER, LineLayout.LEADING, LineLayout.VERTICAL));
			ecWeather.setPreferredSize(new Dimension(240, 210));
			
			String day = "　　" + MyCalendar.getMonth() + "月" + MyCalendar.getDay() + "日　" + MyCalendar.getLunar() + "　" + MyCalendar.getWeekDay(); 
			ecWeather.add(new JCLabel(day));
			ecWeather.add(ecCurrentWeather, LineLayout.START_FILL);
			ecWeather.add(new JCLabel("　　ABigDreamer提醒您："), LineLayout.START_FILL);
			ecWeather.add(ecForecastWeather, LineLayout.START_FILL);

			getContentPane().add(ecWeather);
		} else {
			getContentPane().add(new JCLabel("获取天气失败..."));
		}
	}

	private EmptyComponent getForecastWeatherComponent(Map<String, String> map, String day) {
		EmptyComponent ecOneForecastWeatherFirst = new EmptyComponent();
		ecOneForecastWeatherFirst.setLayout(new GridLayout(1, 2));
		ecOneForecastWeatherFirst.setPreferredSize(new Dimension(80, 40));

		JCLabel lblWeather = new JCLabel();
		lblWeather.setIcon(SwingUtil.getIcon("weather/" + WeatherLabel.getWeatherImage(map.get("icon")) + ".png", 40, 40));

		ecOneForecastWeatherFirst.add(lblWeather);
		ecOneForecastWeatherFirst.add(new JCLabel(day));

		EmptyComponent ecOneForecastWeather3 = new EmptyComponent();
		ecOneForecastWeather3.setLayout(new LineLayout(0, 0, 0, 0, 0, LineLayout.CENTER, LineLayout.LEADING, LineLayout.VERTICAL));
		ecOneForecastWeather3.setPreferredSize(new Dimension(-1, 64));
		ecOneForecastWeather3.add(ecOneForecastWeatherFirst, LineLayout.START);
		ecOneForecastWeather3.add(new JCLabel(map.get("low") + "～" + map.get("high") + "℃"), LineLayout.START);

		return ecOneForecastWeather3;
	}

	public static void main(String[] args) {

		// JCFrame frame = new JCFrame();
		// frame.setSize(200, 200);
		// frame.add(ecOneForecastWeather3);
		// frame.setVisible(true);
	}

}
